#ifndef _GAME_LOGIC_H_
#define _GAME_LOGIC_H_
#include "cocos2d.h"
USING_NS_CC;

#include <vector>

#include "../include/IGameScene.h"
#include "../include/SignalSlot.h"

#include "../config/GameConfig.h"
#include "../config/FishConfig.h"
#include "../config/FishGroup.h"
#include "GameLogicDef.h"
#include "GameUIMgr.h"
#include "GameObject.h"

namespace fishingjoy
{
	namespace logic
	{
		class CGameLogic:public cocos2d::CCObject,public IGameLogic
		{
			scene::IGameScene* m_pGameScene;
			CGameUIMgr mUIMgr;
			std::vector<CFishObject*> m_vecObjectsList;
			std::vector<CBulletObject*> m_vecBulletsList;
			EGameState m_eGameState;
			int m_iSubState;
			float m_fTime;
			float m_fGroupTime;
			std::vector<int> m_vecPercentPool;
			const config::SFishGroupData* m_pGroupData;
			int m_iLevel;
			int m_iTotalMark;
			bool m_SoundOpen;
		public:
			//override base class.
			void update(float dt);
		public:
			//implement interface for IGameLogic
			virtual void enterGame()
			{
				int mark = cocos2d::CCUserDefault::sharedUserDefault()->getIntegerForKey("mark",-1);
				if(mark==-1){
					m_iTotalMark = 100;
				}
				else{
					m_iTotalMark = mark;
				}

				const char* szPlaySceneBg = config::CGameConfig::getPlaySceneBg();
				m_pGameScene->setBackground(szPlaySceneBg);

				m_eGameState = EGS_GAME_PLAY;
				m_iSubState = EGPS_NORMAL;
				m_fTime = 0.0f;
				for(int i = 0; i < config::CGameConfig::getMaxFishNum()/4; ++i){
					createFish();
				}
				
				//emit signal... to notice that game is enter succesfully.
				Event_EnterGameSuccessed();
			}

			virtual void exitGame()
			{
				shutdown();
			}

			virtual void onShoot(float x,float y,float dir);
			virtual void setLevel(int lv){ m_iLevel = lv; }
			virtual int getMark(){ return m_iTotalMark; }
		public:
			bool init(CCScene* pScene);


			void shutdown()
			{
				//to do..

				//exit.
				CCDirector::sharedDirector()->end();
			}

			std::vector<CFishObject*>& getFishs(){ return m_vecObjectsList;}
			void addMark(int mark)
			{
				m_iTotalMark += mark; 
				Event_MarkChange();
				cocos2d::CCUserDefault::sharedUserDefault()->setIntegerForKey("mark",m_iTotalMark);
				cocos2d::CCUserDefault::sharedUserDefault()->flush();
			}
		private:
			CFishObject* createFish();
			CFishObject* createFish(int fid,int x,int y,int speed,int tx,int ty);
			void destroyFish(CFishObject* pObj);
			void destroyBullet( CBulletObject* pObj);

		public:
			virtual void openSound(bool b);
			virtual void playMusic(const char* file,bool bLoop = true);
			virtual int playEffect(const char* file,bool bLoop = false);
			virtual void stopEffect(int h = -1);
		};
	}
}
#endif